# A “Watershed” Moment for Collaboration: 
# The Impacts of Legislation and Network Governance on Environmental Outcomes
# Yixin Liu, Jiho Kim, Tina Nabatchi 2025
# Public Administration Review
# Replication file for project analysis

library(dplyr)
library(ggplot2)
library(egg)
library(cowplot)
library(Rmisc)

element_textbox <- function(...) {
  el <- element_text(...)
  class(el) <- c("element_textbox", class(el))
  el
}

element_grob.element_textbox <- function(element, ...) {
  text_grob <- NextMethod()
  rect_grob <- element_grob(calc_element("strip.background", theme_bw()))
  
  ggplot2:::absoluteGrob(
    grid::gList(
      element_grob(calc_element("strip.background", theme_bw())),
      text_grob
    ),
    height = grid::grobHeight(text_grob), 
    width = grid::unit(1, "npc")
  )
}

# Two decimal places
scaleFUN <- function(x) {
  return(sprintf("%.2f", x))
}

####Data####
# Setup working directory
WD = getwd()
setwd(WD)
df <- read.csv("df_for_projects.csv")

df_cgr1 <- summarySE(df, measurevar="goals", groupvars=c("form"))
names(df_cgr1)[1] <-"structure"

df_cgr2 <- summarySE(df, measurevar="wqi_ratio", groupvars=c("form"))
names(df_cgr2)[1] <-"structure"

df_goal1 <- summarySE(df[df$lead!="other",], measurevar="goals", groupvars=c("lead"))
df_goal1$lead[df_goal1$lead=="citizen_lead"]<-"Citizen group"
df_goal1$lead[df_goal1$lead=="localgov_lead"]<-"Local government"
df_goal1$lead[df_goal1$lead=="state_lead"]<-"State government"
names(df_goal1)[1] <-"structure"

df_goal2 <- summarySE(df[df$lead!="other",], measurevar="wqi_ratio", groupvars=c("lead"))
df_goal2$lead[df_goal2$lead=="citizen_lead"]<-"Citizen group"
df_goal2$lead[df_goal2$lead=="localgov_lead"]<-"Local government"
df_goal2$lead[df_goal2$lead=="state_lead"]<-"State government"
names(df_goal2)[1] <-"structure"

df_total<-rbind(df_cgr1, df_goal1)
df_total$structure <- factor(df_total$structure, 
                             levels = c("Ad-hoc", "CGR", "", 
                                        "Citizen group", "Local government", "State government"))

df_ratio<-rbind(df_cgr2, df_goal2)
df_ratio$structure <- factor(df_ratio$structure, 
                             levels = c("Ad-hoc", "CGR", "",
                                        "Citizen group", "Local government", "State government"))

####Figure 4####
p_total <- ggplot(df_total, aes(x = structure, y = goals, fill = structure, color = structure)) + 
  geom_bar(stat = "identity", width = 0.7, position = position_dodge()) + 
  geom_errorbar(aes(ymin = goals - ci, ymax = goals + ci), width = 0.1, 
                position = position_dodge(0.7)) + 
  labs(x = element_blank(), y = "Total Goals per Project", 
       title = "Goal Multiplicity by Collaboration Forms and Lead Organizations") + 
  theme_article() + 
  scale_x_discrete(drop = FALSE) +
  scale_y_continuous(limits=c(0,6),labels=scaleFUN) +
  theme(legend.position = "none",
        axis.text = element_text(size = 12),
        strip.background = element_blank(),
        strip.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        plot.title = element_textbox(hjust = 0.5, margin = margin(t = 5, b = 5))) + 
  scale_fill_brewer(palette = "Dark2") +
  scale_color_manual(values = c("black", "black", "black", "black", "black"))
p_total

p_ratio <- ggplot(df_ratio, aes(x = structure, y = wqi_ratio, fill = structure, color = structure)) + 
  geom_bar(stat = "identity", width = 0.7, position = position_dodge()) + 
  geom_errorbar(aes(ymin = wqi_ratio - ci, ymax = wqi_ratio + ci), width = 0.1, 
                position = position_dodge(0.7)) + 
  labs(x = element_blank(), y = "Water Quality Goals/Total Goals per Project", 
       title = "Goal Alignment by Collaboration Forms and Lead Organizations") + 
  theme_article() + 
  scale_x_discrete(drop = FALSE) + 
  scale_y_continuous(limits=c(0,0.25),labels=scaleFUN) +
  theme(legend.position = "none",
        axis.text = element_text(size = 12),
        strip.background = element_blank(),
        strip.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        plot.title = element_textbox(hjust = 0.5, margin = margin(t = 5, b = 5))) + 
  scale_fill_brewer(palette = "Dark2") +
  scale_color_manual(values = c("black", "black", "black", "black", "black"))
p_ratio

# combine
plot_figure4<-ggarrange(p_ratio, p_total,
                     ncol = 1, nrow = 2)

plot_figure4<-plot_grid(plot_figure4)
plot_figure4
ggsave(file = "figure4.jpg", width = 10, height = 6, dpi = 666)
ggsave(file = "figure4.eps", width = 10, height = 6, dpi = 666)
